home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 487 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  44 lines

  1. Path: news.th-darmstadt.de!news!enno
  2. From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: beginner question - typecasting
  5. Date: 04 Jan 1996 20:10:12 GMT
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Distribution: world
  8. Message-ID: <ENNO.96Jan4211012@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. References: <4cei1r$s02@sun.cis.smu.edu>
  10. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  11. In-reply-to: dbowman@post.smu.edu's message of 3 Jan 1996 12:31:23 -0600
  12.  
  13. In article <4cei1r$s02@sun.cis.smu.edu> dbowman@post.smu.edu (Damon Bowman) writes:
  14.  
  15.    When you are typecasting, is there any difference between:
  16.  
  17.    a = int(x)
  18.    and
  19.    a = (int) x
  20.  
  21.    My book (Teach Yourself Visual C++ 1.5 in 21 Days, Revised Edition, by
  22.    Shammas) says both forms are supported, but doesnÆt say which should
  23.    be used, or if it matters.
  24.  
  25.    The specific example given is:
  26.  
  27.    int i = 2;
  28.    float a, b;
  29.    a = float(i);
  30.    b = (float) i;
  31.  
  32.    In this example, would a and b return the same value and have the same
  33.    data type (float)?
  34.  
  35. Yes.
  36. For builtin-types there is no difference between these two variants.
  37. Because 'int' and 'float' are builtin-types both statements have the
  38. same effect.
  39. For an arbitary user-defined class T the former uses a ctor with an
  40. argument of typeof(x), while the latter expects a conversion operator.
  41.  
  42.         Enno
  43.  
  44.